using System;
using Unity.Collections;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty
{
    /*
     * 注意！！！默认地图是XZ平面轴对齐的，没有旋转！！！
     * 使用步骤:
     * 1.将地图拖入地图纹理中
     * 2.点击"显示/刷新预览地图"按钮，创建地图预览
     * 3.通过MapDataComponent组件的编辑器面板调整地图缩放和偏移
     * 4.删除预览地图 (可选)
     * 5.导出地图数据
     */
    public class MapSettingComponent : MonoBehaviour
    {
        [Header("----------------数据信息----------------")]
        [Header("地图包围盒数据")]
        [SerializeField]
        [ReadOnly]
        private Bounds _Bounds;

        // LB
        public Vector3 GetMapLeftBottomWorldPosition()
        {
            return new Vector3(_Bounds.center.x-_Bounds.extents.x*0.5f, 0, _Bounds.center.z-_Bounds.extents.z*0.5f);
        }
        // LT
        public Vector3 GetMapLeftTopWorldPosition()
        {
            return new Vector3(_Bounds.center.x-_Bounds.extents.x*0.5f, 0, _Bounds.center.z+_Bounds.extents.z*0.5f);
        }
        // RB
        public Vector3 GetMapRightBottomWorldPosition()
        {
            return new Vector3(_Bounds.center.x+_Bounds.extents.x*0.5f, 0, _Bounds.center.z-_Bounds.extents.z*0.5f);
        }
        // RT
        public Vector3 GetMapRightTopWorldPosition()
        {
            return new Vector3(_Bounds.center.x+_Bounds.extents.x*0.5f, 0, _Bounds.center.z+_Bounds.extents.z*0.5f);
        }

        [Space]
        [Space]
        [Header("----------------编辑器数据----------------")]
        [Header("预设导出路径")]
        public string _ExportPath = "Assets/ResourcesAssets/Hanging/Prefabs/Map/MapSetting.prefab";
        [Header("地图纹理")]
        public Sprite _MapSprite;
        [Header("地图预览")]
        public SpriteRenderer _PreviewSpriteRenderer;
        [Header("地图偏移")]
        public Vector3 _MapOffset = new Vector3(0, 1, 0);
        [Header("地图缩放")]
        [SerializeField] 
        [Range(0.5f, 10f)]
        public float _OriginMapScale = 1f;
        
        public void SetBounds(Vector3 center, Vector3 extents)
        {
            _Bounds.center = center;
            _Bounds.extents = extents;
        }
        
        private void OnDrawGizmosSelected()
        {
            Color old = Gizmos.color;
            Gizmos.color = new Color(0.0f, 1f, 0f, 0.3f);
            Gizmos.DrawCube(_Bounds.center, _Bounds.extents);
            
            Gizmos.color = new Color(1f, 1f, 0f, 1f);
            // LB
            Gizmos.DrawSphere(GetMapLeftBottomWorldPosition(), 20);
            // LT
            Gizmos.DrawSphere(GetMapLeftTopWorldPosition(), 20);
            // RB
            Gizmos.DrawSphere(GetMapRightBottomWorldPosition(), 20);
            // RT
            Gizmos.DrawSphere(GetMapRightTopWorldPosition(), 20);
            
            Gizmos.color = old;
        }
        
    }
}